package main

import "fmt"

type User struct {
	Id   string
	Name string
}

func fa(s []int, n int) int {
	i := n
	for i = 0; i < len(s); i++ {
	}
	return i
}

func fb(s []int, n int) int {
	i := n
	for i = range s {
	}
	return i
}

func main() {
	// https://gfw.go101.org/article/details.html#different-loops
	s := []int{2, 3, 5, 7, 11, 13}
	fmt.Println(fa(s, -1), fb(s, -1)) // 6 5
	s = nil
	fmt.Println(fa(s, -1), fb(s, -1)) // 0 -1

	fmt.Println("====")

	arr := []User{
		{Id: "id1"}, {Id: "id2"},
	}
	m1 := make(map[string]*User)
	m2 := make(map[string]*User)
	for i := 0; i < len(arr); i++ {
		a := arr[i]
		m1[a.Id] = &a

		b := &arr[i]
		m2[a.Id] = b
	}

	m1["id1"].Name = "张三" // m1的都没法生效
	m2["id2"].Name = "李四"

	/*
		{id1 }
		{id2 李四}
	*/
	fmt.Println(arr[0])
	fmt.Println(arr[1])

	// arr 的地址与 m2["id1"] 一致
	fmt.Printf("%p %p %p \n", arr[0], arr[1], arr)
	fmt.Printf("%p %p \n", m1["id1"], m1["id2"])
	fmt.Printf("%p %p \n", m2["id1"], m2["id2"])

	fmt.Println("======")

	users := []User{
		{Id: "id1"}, {Id: "id2"},
	}
	for _, user := range users {
		user.Name = "无效修改"
	}
	fmt.Println(users[0]) // {id1 }

	for i, user := range users {
		if user.Id == "id1" {
			users[i].Name = "index有效修改"
		}
	}

	fmt.Println(users[0]) // {id1 index有效修改}

	userPointers := []*User{
		{Id: "id1"}, {Id: "id2"},
	}
	for _, user := range userPointers {
		user.Name = "指针有效修改"
	}
	fmt.Println(userPointers[0]) // &{id1 指针有效修改}

}
